package org.example.framework.protocol.http;

import cn.hutool.http.HttpRequest;
import org.example.framework.Invocation;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * @author shenguangyang
 */
public class HttpClient {

    public Object send(String hostname, Integer port, Invocation invocation) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream;
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(invocation);
            byte[] bytes = byteArrayOutputStream.toByteArray();

            String url = "http://" + hostname + ":" + port + "/";
            HttpRequest httpRequest = HttpRequest.post(url).body(bytes);
            return httpRequest.execute().body();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
